/*
do C:\Xu_Lin\Paper\Lee_Li\2011\Stata\sub_data_G10.do

 */
clear
set more off
log using C:\Xu_Lin\Paper\Lee_Li\2011\Stata\sub_data_G10.log,replace
#delimit ;

set mem 900m;

/*define number of group gsize*/
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_aid, clear;

egen ggroup3 = group(scid_own grade_own);
sort ggroup3;
local gsize=ggroup3[_N];
sum;

foreach i of num 1/`gsize' 
{;

use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_aid, clear;

egen ggroup2 = group(scid_own grade_own);
drop if ggroup2~=`i';
keep aid ggroup2;
sort ggroup2 aid;
gen row=_n;
sort aid;
save C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_row, replace;

use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\id_friends,clear;
drop if ggroup~=`i';
sort aid;
merge aid using C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_row;
drop _merge;
sort fid;
save C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\id_variables_row, replace;

use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_row, clear;
keep aid row;
rename aid fid;
rename row col;
sort fid;

save C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_col, replace;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\id_variables_row,clear;
merge fid using C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_col;

drop if _merge==2; /* never be listed as frd by other */
drop _merge;
sort aid;


save C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\id_variables_all, replace; 

gen wij=1;
replace wij=0 if aid==fid;

sort row;
save C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\id_variables_all, replace; 
keep row col wij;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\weight\w_`i'.txt,replace;

};


/* data file*/

use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\variables_1,clear;

keep aid scid grade 
age yr_school   male    black   
Asian   hisp    race_other both_par 
less_hs more_hs momedu_miss Welfare 
momjob_miss Prof job_other  sport gpa; 

egen ggroup0 = group(scid grade);
sort ggroup0 aid;
save C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data,replace;

/*part1*/

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data,clear;
drop if ggroup0~=`i';
keep age    yr_school   male    black;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part1\data_part1_`i'.txt,replace;

};

/*part2*/

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data,clear;
drop if ggroup0~=`i';
keep Asian  hisp    race_other both_par;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part2\data_part2_`i'.txt,replace;

};

/*part3*/

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data,clear;
drop if ggroup0~=`i';
keep less_hs    more_hs momedu_miss Welfare;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part3\data_part3_`i'.txt,replace;

};

/*part4*/

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data,clear;
drop if ggroup0~=`i';
keep momjob_miss    Prof job_other  sport gpa;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part4\data_part4_`i'.txt,replace;

};

/**************************************************** add school dummies here*******/
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data,clear;
/* 127 schools, 126 dummies. */
gen S1=0;
gen S2=0;
gen S3=0;
gen S4=0;
gen S5=0;
gen S6=0;
gen S7=0;
gen S8=0;
gen S9=0;
gen S10=0;
gen S11=0;
gen S12=0;
gen S13=0;
gen S14=0;
gen S15=0;
gen S16=0;
gen S17=0;
gen S18=0;
gen S19=0;
gen S20=0;
gen S21=0;
gen S22=0;
gen S23=0;
gen S24=0;
gen S25=0;
gen S26=0;
gen S27=0;
gen S28=0;
gen S29=0;
gen S30=0;
gen S31=0;
gen S32=0;
gen S33=0;
gen S34=0;
gen S35=0;
gen S36=0;
gen S37=0;
gen S38=0;
gen S39=0;
gen S40=0;
gen S41=0;
gen S42=0;
gen S43=0;
gen S44=0;
gen S45=0;
gen S46=0;
gen S47=0;
gen S48=0;
gen S49=0;
gen S50=0;
gen S51=0;
gen S52=0;
gen S53=0;
gen S54=0;
gen S55=0;
gen S56=0;
gen S57=0;
gen S58=0;
gen S59=0;
gen S60=0;
gen S61=0;
gen S62=0;
gen S63=0;
gen S64=0;
gen S65=0;
gen S66=0;
gen S67=0;
gen S68=0;
gen S69=0;
gen S70=0;
gen S71=0;
gen S72=0;
gen S73=0;
gen S74=0;
gen S75=0;
gen S76=0;
gen S77=0;
gen S78=0;
gen S79=0;
gen S80=0;
gen S81=0;
gen S82=0;
gen S83=0;
gen S84=0;
gen S85=0;
gen S86=0;
gen S87=0;
gen S88=0;
gen S89=0;
gen S90=0;
gen S91=0;
gen S92=0;
gen S93=0;
gen S94=0;
gen S95=0;
gen S96=0;
gen S97=0;
gen S98=0;
gen S99=0;
gen S100=0;
gen S101=0;
gen S102=0;
gen S103=0;
gen S104=0;
gen S105=0;
gen S106=0;
gen S107=0;
gen S108=0;
gen S109=0;
gen S110=0;
gen S111=0;
gen S112=0;
gen S113=0;
gen S114=0;
gen S115=0;
gen S116=0;
gen S117=0;
gen S118=0;
gen S119=0;
gen S120=0;
gen S121=0;
gen S122=0;
gen S123=0;
gen S124=0;
gen S125=0;
gen S126=0;

replace S1= 1 if    scid==          3  ;
replace S2= 1 if    scid==          6  ;
replace S3= 1 if    scid==          7  ;
replace S4= 1 if    scid==          8  ;
replace S5= 1 if    scid==          9  ;
replace S6= 1 if    scid==         10  ;
replace S7= 1 if    scid==         12  ;
replace S8= 1 if    scid==         13  ;
replace S9= 1 if    scid==         14  ;
replace S10=1 if    scid==         15  ;
replace S11=1 if    scid==         16  ;
replace S12=1 if    scid==         17  ;
replace S13=1 if    scid==         18  ;
replace S14=1 if    scid==         19  ;
replace S15=1 if    scid==         20  ;
replace S16=1 if    scid==         21  ;
replace S17=1 if    scid==         22  ;
replace S18=1 if    scid==         23  ;
replace S19=1 if    scid==         24  ;
replace S20=1 if    scid==         25  ;
replace S21=1 if    scid==         26  ;
replace S22=1 if    scid==         27  ;
replace S23=1 if    scid==         28  ;
replace S24=1 if    scid==         29  ;
replace S25=1 if    scid==         30  ;
replace S26=1 if    scid==         31  ;
replace S27=1 if    scid==         33  ;
replace S28=1 if    scid==         34  ;
replace S29=1 if    scid==         35  ;
replace S30=1 if    scid==         39  ;
replace S31=1 if    scid==         40  ;
replace S32=1 if    scid==         41  ;
replace S33=1 if    scid==         42  ;
replace S34=1 if    scid==         43  ;
replace S35=1 if    scid==         44  ;
replace S36=1 if    scid==         45  ;
replace S37=1 if    scid==         47  ;
replace S38=1 if    scid==         49  ;
replace S39=1 if    scid==         50  ;
replace S40=1 if    scid==         52  ;
replace S41=1 if    scid==         53  ;
replace S42=1 if    scid==         54  ;
replace S43=1 if    scid==         55  ;
replace S44=1 if    scid==         56  ;
replace S45=1 if    scid==         57  ;
replace S46=1 if    scid==         58  ;
replace S47=1 if    scid==         59  ;
replace S48=1 if    scid==         60  ;
replace S49=1 if    scid==         62  ;
replace S50=1 if    scid==         64  ;
replace S51=1 if    scid==         65  ;
replace S52=1 if    scid==         66  ;
replace S53=1 if    scid==         67  ;
replace S54=1 if    scid==         68  ;
replace S55=1 if    scid==         71  ;
replace S56=1 if    scid==         72  ;
replace S57=1 if    scid==         73  ;
replace S58=1 if    scid==         74  ;
replace S59=1 if    scid==         75  ;
replace S60=1 if    scid==         76  ;
replace S61=1 if    scid==         77  ;
replace S62=1 if    scid==         78  ;
replace S63=1 if    scid==         79  ;
replace S64=1 if    scid==         82  ;
replace S65=1 if    scid==         83  ;
replace S66=1 if    scid==         84  ;
replace S67=1 if    scid==         85  ;
replace S68=1 if    scid==         86  ;
replace S69=1 if    scid==         87  ;
replace S70=1 if    scid==         90  ;
replace S71=1 if    scid==         91  ;
replace S72=1 if    scid==         92  ;
replace S73=1 if    scid==         93  ;
replace S74=1 if    scid==        109  ;
replace S75=1 if    scid==        114  ;
replace S76=1 if    scid==        116  ;
replace S77=1 if    scid==        117  ;
replace S78=1 if    scid==        118  ;
replace S79=1 if    scid==        119  ;
replace S80=1 if    scid==        120  ;
replace S81=1 if    scid==        121  ;
replace S82=1 if    scid==        122  ;
replace S83=1 if    scid==        123  ;
replace S84=1 if    scid==        127  ;
replace S85=1 if    scid==        133  ;
replace S86=1 if    scid==        138  ;
replace S87=1 if    scid==        139  ;
replace S88=1 if    scid==        141  ;
replace S89=1 if    scid==        142  ;
replace S90=1 if    scid==        143  ;
replace S91=1 if    scid==        144  ;
replace S92=1 if    scid==        145  ;
replace S93=1 if    scid==        146  ;
replace S94=1 if    scid==        147  ;
replace S95=1 if    scid==        149  ;
replace S96=1 if    scid==        150  ;
replace S97=1 if    scid==        152  ;
replace S98=1 if    scid==        153  ;
replace S99=1 if    scid==        154  ;
replace S100=1 if   scid==        155  ;
replace S101=1 if   scid==        157  ;
replace S102=1 if   scid==        158  ;
replace S103=1 if   scid==        160  ;
replace S104=1 if   scid==        162  ;
replace S105=1 if   scid==        164  ;
replace S106=1 if   scid==        166  ;
replace S107=1 if   scid==        167  ;
replace S108=1 if   scid==        168  ;
replace S109=1 if   scid==        170  ;
replace S110=1 if   scid==        171  ;
replace S111=1 if   scid==        172  ;
replace S112=1 if   scid==        173  ;
replace S113=1 if   scid==        174  ;
replace S114=1 if   scid==        176  ;
replace S115=1 if   scid==        177  ;
replace S116=1 if   scid==        185  ;
replace S117=1 if   scid==        186  ;
replace S118=1 if   scid==        187  ;
replace S119=1 if   scid==        191  ;
replace S120=1 if   scid==        193  ;
replace S121=1 if   scid==        259  ;
replace S122=1 if   scid==        267  ;
replace S123=1 if   scid==        268  ;
replace S124=1 if   scid==        269  ;
replace S125=1 if   scid==        270  ;
replace S126=1 if   scid==        271  ;

save C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,replace;
                               
foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S1 S2 S3 S4;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part5\data_part5_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S5 S6 S7 S8;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part6\data_part6_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S9 S10 S11 S12;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part7\data_part7_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S13 S14 S15 S16;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part8\data_part8_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S17 S18 S19 S20;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part9\data_part9_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S21 S22 S23 S24;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part10\data_part10_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S25 S26 S27 S28;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part11\data_part11_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S29 S30 S31 S32;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part12\data_part12_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S33 S34 S35 S36;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part13\data_part13_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S37 S38 S39 S40;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part14\data_part14_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S41 S42 S43 S44;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part15\data_part15_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S45 S46 S47 S48;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part16\data_part16_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S49 S50 S51 S52;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part17\data_part17_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S53 S54 S55 S56;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part18\data_part18_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S57 S58 S59 S60;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part19\data_part19_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S61 S62 S63 S64;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part20\data_part20_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S65 S66 S67 S68;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part21\data_part21_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S69 S70 S71 S72;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part22\data_part22_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S73 S74 S75 S76;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part23\data_part23_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S77 S78 S79 S80;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part24\data_part24_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S81 S82 S83 S84;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part25\data_part25_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S85 S86 S87 S88;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part26\data_part26_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S89 S90 S91 S92;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part27\data_part27_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S93 S94 S95 S96;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part28\data_part28_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S97 S98 S99 S100;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part29\data_part29_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S101 S102 S103 S104;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part30\data_part30_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S105 S106 S107 S108;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part31\data_part31_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S109 S110 S111 S112;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part32\data_part32_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S113 S114 S115 S116;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part33\data_part33_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S117 S118 S119 S120;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part34\data_part34_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S121 S122 S123 S124;
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part35\data_part35_`i'.txt,replace;

};

foreach i of num 1/`gsize'  
{;
use C:\Xu_Lin\Paper\Lee_Li\2011\data_temp\data_D,clear;
drop if ggroup0~=`i';
keep S125 S126 /* S127 S128 */;  /* only have 126 dummies.*/
outfile using C:\Xu_Lin\Paper\Lee_Li\2011\data_final\part36\data_part36_`i'.txt,replace;

};

set more on;
#delimit cr

log close
clear
